import { getcourses, getCoursesAds } from '@/api/courses.js'
export default {
  namespaced: true,
  state: () => ({
    professionId: 0,
    // 商品列表
    recommends: {},
    // 选中的商品(科目筛选)
    currentProducts: {},
    // banner广告
    ads: []
  }),
  mutations: {
    setCurrentProducts(state, data) {
      state.currentProducts = data
    },
    setProfessionId(state, val) {
      // 只用两个参数 一个时state ,另一个是可以 字符串、对象、数组等
      state.professionId = val
    },
    setRecommend(state, data) {
      state.recommends = data
    },
    setAds(state, data) {
      state.ads = data
    }
  },
  actions: {
    async getRecommends({ commit }, params) {
      let res = await getcourses(params).catch(err => {
        return {
          errcode: 0,
          data: { recommend: [] }
        }
      })
      let {
        data: { recommend = [] }
      } = res
      let currentProfessionId = params.profession_id
      // 将商品数据按照专业、科目分类（只处理当前专业的数据,其他专业的数据全为空）
      let data = {}
      recommend.forEach((item, index) => {
        let {
          lm_name,
          profession_id,
          recommend_products,
          recommend_type,
          industry_id
        } = item
        if (recommend_type === 'industry') {
          //行业类型时,将profession_id修改为行业-专业模式
          profession_id = `${industry_id}-${profession_id}`
        }
        let professionObj = { lm_name, profession_id, subjects: [] }
        // 不是当前专业的商品数据直接置为空
        if (profession_id !== currentProfessionId) {
          data[profession_id] = item
        } else {
          let subjectProductMap = {}
          //该专业下全部商品
          // 先将数据按科目分类处理成 id:{} 格式
          recommend_products.forEach(productItem => {
            if (subjectProductMap.hasOwnProperty(productItem.subject_id)) {
              subjectProductMap[productItem.subject_id].push(productItem)
            } else {
              subjectProductMap[productItem.subject_id] = []
              subjectProductMap[productItem.subject_id].push(productItem)
            }
          })
          let arr = []
          let allProducts = {
            products: recommend_products,
            subject_id: '0',
            subject_name: '全部'
          }
          commit('setCurrentProducts', recommend_products)
          arr.push(allProducts)
          // 再将科目商品id:{}格式转换成科目对象数组，并放到专业的subjects字段
          professionObj.subjects = Object.values(subjectProductMap).reduce(
            (sum, items) => {
              let obj = {}
              obj.subject_id = items[0].subject_id
              obj.subject_name = items[0].subject_name
              obj.products = items
              sum.push(obj)
              return sum
            },
            arr
          )
          data[profession_id] = professionObj
        }
      }, [])
      commit('setRecommend', data)
    },
    async getAds({ commit }, params) {
      let res = await getCoursesAds(params)
      if (+res.errcode !== 0) {
        commit('setAds', [])
      } else {
        res.data ? commit('setAds', res.data.list) : commit('setAds', [])
      }
    }
  },
  getters: {}
}
